//
//  Http.swift
//  微博项目
//
//  Created by admin on 16/3/7.
//  Copyright © 2016年 jin. All rights reserved.
//

import UIKit

class Http: NSObject {
    class func Post(URLString: String, parameters: AnyObject?, success: ((NSURLSessionDataTask, AnyObject?) -> Void)?, failure: ((NSURLSessionDataTask?, NSError) -> Void)?)
    {
        let manager = AFHTTPSessionManager()
        var acceptTable:Set<String>? = manager.responseSerializer.acceptableContentTypes
        acceptTable?.insert("text/plain")
        manager.responseSerializer.acceptableContentTypes = acceptTable
        manager.POST(URLString, parameters: parameters, success: { (task:NSURLSessionDataTask, data:AnyObject?) -> Void in
            success?(task,data)
        }) { (task:NSURLSessionDataTask?, error:NSError) -> Void in
            failure?(task,error)
        }
    }
    class func Get(URLString: String, parameters: AnyObject?, success: ((NSURLSessionDataTask, AnyObject?) -> Void)?, failure: ((NSURLSessionDataTask?, NSError) -> Void)?)
    {
        let manager = AFHTTPSessionManager()
        manager.GET(URLString, parameters: parameters, success: { (task:NSURLSessionDataTask, data:AnyObject?) -> Void in
                success?(task,data)
            }) { (task:NSURLSessionDataTask?, error:NSError) -> Void in
                failure?(task,error)
        }
    }
    class func upload(URLString: String, parameters: AnyObject?, datas: [UploadDataParameter], successParameter: ((NSURLSessionDataTask, AnyObject?) -> Void)?, failureParameter: ((NSURLSessionDataTask?, NSError) -> Void)?)
    {
        let manager = AFHTTPSessionManager()
        var acceptTable:Set<String>? = manager.responseSerializer.acceptableContentTypes
        acceptTable?.insert("text/plain")
        manager.responseSerializer.acceptableContentTypes = acceptTable
        manager.POST(URLString, parameters: parameters, constructingBodyWithBlock: { (uploadData:AFMultipartFormData) -> Void in
                datas.forEach({ (data:UploadDataParameter) -> () in
                    uploadData.appendPartWithFileData(data.data, name: data.name, fileName: data.fileName, mimeType: data.mimeType)
                })
            }, progress: { (progress:NSProgress) -> Void in
                
            }, success: { (task:NSURLSessionDataTask, data:AnyObject?) -> Void in
                successParameter!(task,data)
            }) { (task:NSURLSessionDataTask?, error:NSError) -> Void in
                failureParameter!(task,error)
        }
    }
}
